﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace XNAGame_Lab1
{
    public class MyXNAButton : VisibleGame
    {
        int _Cost;
        
        
        public int Cost
        {
            get { return _Cost; }
            set { _Cost = value; }
        }

        public MyXNAButton(ContentManager content, List<string> strTexture, int nSprites, Vector2 topleft, Vector2 size)
        {
            InitMyXNAButton(content, strTexture, nSprites, ref topleft, ref size);


        }
        public MyXNAButton() { }
        //  /abc/dass/hinh1
        private void InitMyXNAButton(ContentManager content, List<string> strTexture, int nSprites, ref Vector2 topleft, ref Vector2 size)
        {
            TopLeft = topleft;
            Size = size;

            UpdateImg(content, strTexture,  nSprites,  topleft);

        }

        public void UpdateImg(ContentManager content, List<string> strTexture,  int nSprites,  Vector2 topleft)
        {
            nSprites = strTexture.Count;
            Sprites = new List<MySprites2D>();
            Texture2D[] texture = new Texture2D[nSprites];
            for (int i = 0; i < nSprites; i++)
                texture[i] = content.Load<Texture2D>(strTexture[i]);
            MySprites2D mySprite = new MySprites2D(texture, topleft);
            mySprite.Size = Size;            
            Sprites.Add(mySprite);
            this.nSprites = Sprites.Count;
        }
        public MyXNAButton(ContentManager content, string refix, int nSprites, Vector2 topleft, Vector2 size)
        {
            InitWithRefix(content, refix, nSprites, ref topleft, ref size);

        }

        public void InitWithRefix(ContentManager content, string refix, int nSprites, ref Vector2 topleft, ref Vector2 size)
        {
            List<string> strTexture = new List<string>(nSprites);
            for (int i = 0; i < nSprites; i++)
                strTexture.Add(refix + (i).ToString("00"));
            InitMyXNAButton(content, strTexture, nSprites, ref topleft, ref size);

        }
        public MyXNAButton(ContentManager content, string strXML)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(strXML);
            XmlNodeList root = doc.GetElementsByTagName("buttons");
            XmlNode parent = root.Item(0);
            XmlNode button = parent.ChildNodes.Item(0);
            XmlNode texture = button.ChildNodes.Item(4);
            List<string> strTexture = new List<string>(15);
            //            int nSprites = 1;
            Vector2 topleft = new Vector2();
            Vector2 size = new Vector2();
            XmlNode X = button.ChildNodes.Item(0);
            XmlNode Y = button.ChildNodes.Item(1);
            XmlNode W = button.ChildNodes.Item(2);
            XmlNode H = button.ChildNodes.Item(3);

            topleft.X = int.Parse(X.InnerText);
            topleft.Y = int.Parse(Y.InnerText);
            size.X = int.Parse(W.InnerText);
            size.Y = int.Parse(H.InnerText);

            for (int i = 0; i < 15; i++)
            {
                XmlNode texture1 = texture.ChildNodes.Item(i);
                strTexture[i] = texture1.InnerText;

            }
            InitMyXNAButton(content, strTexture, nSprites, ref topleft, ref size);
            //            XmlNodeList button = buttons.("button");


        }

        public override void Update(GameTime gameTime)
        {
          
        
            base.Update(gameTime);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="strTexture"></param>
        /// <param name="nSprites"></param>
        /// <param name="topleft"></param>
        /// <param name="size">kich thuoc </param>
        public  virtual void UpdateImg(ContentManager content, string strTexture, int nSprites, Vector2 topleft, Vector2 size)
        {
            InitWithRefix(content, strTexture, nSprites, ref topleft, ref size);
            //            State = ButtonStatus.not;
        }
    }
}
